package cn.edu.klmy.controller;

import cn.edu.klmy.dao.IBlackListDao;
import cn.edu.klmy.dao.ITeacherDao;
import cn.edu.klmy.models.blacklist.BlackList;
import cn.edu.klmy.models.student.Student;
import cn.edu.klmy.models.teacher.Teacher;
import cn.edu.klmy.spring.security.core.userdetails.CourseSystemUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 顾伟
 *         创建日期：13-5-1
 */
@Controller
@RequestMapping("/blackList")
public class BlackListController {
    @Autowired
    private IBlackListDao blackListDao;

    @Autowired
    private ITeacherDao teacherDao;

    @RequestMapping("/list/students")
    @ResponseBody
    public List<Student> loadBlackListStudents(){
        String teacherIDNo = ((CourseSystemUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getUser().getUserIdNo();
        Teacher teacher= teacherDao.getTeacher("TEACHER_ID_NO", "'"+teacherIDNo+"'");
        int teacherId=0;
        if(teacher!=null)
            teacherId=teacher.getId();
        int blackListId=blackListDao.addBlackList(teacherId);
        return blackListDao.getBlackListStudents(blackListId);
    }

    @RequestMapping("/list/notIn/student")
    @ResponseBody
    public List<Student> loadNunBlackListStudents() {
        String teacherIDNo = ((CourseSystemUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getUser().getUserIdNo();
        Teacher teacher= teacherDao.getTeacher("TEACHER_ID_NO", "'"+teacherIDNo+"'");
        int teacherId=0;
        if(teacher!=null)
            teacherId=teacher.getId();
        int blackListId=blackListDao.addBlackList(teacherId);
        return blackListDao.getNunBlackListStudents(blackListId);
    }

    @RequestMapping("/add/{studentId}")
    @ResponseBody
    public boolean addStudentToBlackList(@PathVariable int studentId) {
        String teacherIDNo = ((CourseSystemUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getUser().getUserIdNo();
        Teacher teacher= teacherDao.getTeacher("TEACHER_ID_NO", "'"+teacherIDNo+"'");
        int teacherId=0;
        if(teacher!=null)
            teacherId=teacher.getId();
        int blackListId=blackListDao.addBlackList(teacherId);
        blackListDao.addStudentToBlackList(blackListId, studentId);
        return true;
    }

    @RequestMapping("/delete/{studentId}")
    @ResponseBody
    public boolean deleteStudentFromBlackList( @PathVariable int studentId) {
        String teacherIDNo = ((CourseSystemUser) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal()).getUser().getUserIdNo();
        Teacher teacher= teacherDao.getTeacher("TEACHER_ID_NO", "'"+teacherIDNo+"'");
        int teacherId=0;
        if(teacher!=null)
            teacherId=teacher.getId();
        int blackListId=blackListDao.addBlackList(teacherId);
        blackListDao.deleteStudentFromBlackList(blackListId, studentId);
        return true;
    }
}
